package com.jaks.xpense;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.jaks.xpense.data.Tags;
import com.jaks.xpense.data.entities.TableEntity;
import com.jaks.xpense.data.entities.Tag;

public class ManageTagsActivity extends ListActivity implements View.OnCreateContextMenuListener {

	private Tags tags;
	private Cursor orderedTags;

	private TouchInterceptor tagsView;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.managetags);
		registerForContextMenu(getListView());

		tags = new Tags(this);

		tagsView = (TouchInterceptor) this.findViewById(android.R.id.list);
		tagsView.setOnCreateContextMenuListener(this);
		tagsView.setDropListener(itemDropListener);

		// Fast Fling removes the item. Since Tags are the core building blocks
		// now blocking this
		// tagsView.setRemoveListener(itemRemoveListener);
	}

	@Override
	protected void onStart() {
		super.onStart();

		orderedTags = tags.readAll();
		startManagingCursor(orderedTags);

		populate();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		tags.close();
	}

	private void populate() {

		String[] tableColumns = new String[] { Tag.Name };
		int[] columnNames = new int[] { R.id.tagname };

		SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(this, R.layout.tagrow, orderedTags,
		    tableColumns, columnNames);

		setListAdapter(listAdapter);
	}

	private void createTag() {
		Intent intent = new Intent(this, EditTagActivity.class);
		startActivity(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		menu.add(Menu.NONE, R.string.newTag, Menu.NONE, R.string.newTag).setIcon(
		    android.R.drawable.ic_menu_add);
		return true;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);

		menu.setHeaderTitle(R.string.manageTags);
		menu.setHeaderIcon(null);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.delete, menu);
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {

		switch (item.getItemId()) {
			case R.string.newTag:
				createTag();
				return true;
		}
		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.string.newTag:
				createTag();
				return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.deleteItem:
				AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
				tags.delete(info.id);
				orderedTags.requery();
				return true;
		}
		return super.onContextItemSelected(item);
	}

	@Override
	protected void onListItemClick(ListView listView, View view, int position, long id) {
		super.onListItemClick(listView, view, position, id);
		Intent intent = new Intent(this, EditTagActivity.class);
		intent.putExtra(TableEntity.Id, id);

		startActivity(intent);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		orderedTags.requery();
	}

	private final TouchInterceptor.DropListener itemDropListener = new TouchInterceptor.DropListener() {
		@Override
		public void drop(int from, int to) {
			tags.updateTagOrder(from, to);
			orderedTags.requery();
		}
	};

	// private final TouchInterceptor.RemoveListener itemRemoveListener = new
	// TouchInterceptor.RemoveListener() {
	// @Override
	// public void remove(int which) {
	// orderedTags.moveToPosition(which);
	// long rowId =
	// orderedTags.getLong(orderedTags.getColumnIndexOrThrow(TableEntity.Id));
	// tags.delete(rowId);
	// orderedTags.requery();
	// }
	// };
}
